package 模拟题.最大子矩阵;

public class Main {
    public static int[][] a = {
            {6, 9, 8, 5, 9, 2, 4, 1, 8, 3, 9, 3, 8, 7, 8, 6, 8, 9, 4, 1},
            {1, 7, 6, 1, 5, 8, 7, 6, 9, 6, 3, 1, 3, 1, 7, 5, 9, 2, 8, 4},
            {3, 7, 3, 4, 7, 3, 4, 8, 3, 2, 6, 6, 2, 7, 4, 8, 3, 4, 8, 5},
            {5, 3, 6, 7, 1, 2, 5, 6, 5, 5, 6, 1, 6, 7, 8, 6, 4, 7, 4, 3},
            {1, 6, 1, 2, 1, 6, 8, 6, 9, 2, 7, 4, 3, 2, 3, 2, 9, 4, 7, 9},
            {1, 3, 5, 4, 7, 4, 1, 3, 3, 4, 9, 9, 6, 2, 7, 7, 3, 4, 4, 7},
            {2, 7, 9, 7, 9, 9, 4, 5, 9, 2, 9, 8, 4, 8, 8, 2, 4, 6, 8, 7},
            {5, 3, 7, 7, 6, 9, 8, 3, 3, 4, 6, 8, 3, 8, 7, 9, 1, 3, 7, 9},
            {5, 6, 4, 9, 3, 4, 2, 1, 3, 6, 5, 3, 6, 5, 7, 1, 7, 7, 4, 5},
            {2, 1, 9, 2, 4, 3, 7, 9, 2, 9, 3, 8, 7, 2, 6, 1, 1, 3, 8, 2},
            {9, 3, 9, 1, 9, 3, 5, 3, 2, 1, 6, 2, 4, 3, 5, 6, 1, 2, 7, 7},
            {5, 4, 2, 9, 6, 1, 4, 4, 7, 6, 3, 9, 6, 9, 2, 5, 7, 7, 8, 8},
            {9, 6, 2, 3, 3, 9, 7, 2, 5, 1, 3, 7, 9, 4, 7, 3, 2, 9, 3, 3},
            {8, 1, 4, 4, 3, 4, 9, 4, 5, 3, 3, 1, 2, 9, 9, 3, 9, 9, 7, 5},
            {6, 1, 1, 7, 1, 8, 8, 2, 9, 8, 8, 8, 7, 7, 5, 9, 3, 4, 9, 9},
            {6, 1, 2, 1, 6, 8, 6, 8, 8, 9, 5, 7, 2, 1, 3, 4, 8, 5, 2, 2},
            {5, 5, 4, 8, 5, 3, 4, 5, 9, 5, 9, 2, 9, 4, 7, 2, 6, 8, 9, 6},
            {3, 2, 1, 2, 4, 9, 6, 3, 3, 1, 8, 2, 4, 2, 5, 5, 4, 9, 2, 2},
            {1, 3, 5, 9, 3, 6, 4, 7, 1, 9, 1, 9, 3, 4, 2, 7, 2, 6, 9, 6},
            {5, 6, 4, 3, 6, 8, 9, 5, 9, 4, 4, 9, 1, 9, 8, 9, 9, 2, 4, 6}};
    public static void main(String[] args) {
        int max=0;
        //第一层循环和第二层循环确定第一个点的值
        //第三层和第四层循环确定第二个点的值
        for (int i = 0; i <20 ; i++) {
            for (int j = 0; j <20 ; j++) {
                for (int k = i; k <20 ; k++) {
                    for (int l = j; l <20; l++) {
                        if(k-i==4&&l-j==4){ //只计算5*5矩阵和
                            int sum = sum(i, j, k, l);
                            max=Math.max(max,sum);
                        }
                    }
                }
            }
        }
        System.out.println(max);

    }
    //计算从(i,j)到(k,l)的矩阵元素和
    public static int sum(int i,int j,int k,int l){
        int sum=0;
        for (int m = i; m <=k ; m++) {
            for (int n = j; n <=l ; n++) {
                sum+=a[m][n];
            }
        }
        return sum;
    }
}
